package net.lab1024.sa.admin.module.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfCategory;
import net.lab1024.sa.admin.module.workflow.domain.form.*;
import net.lab1024.sa.admin.module.workflow.service.WfCategoryService;
import net.lab1024.sa.admin.module.workflow.service.WfModelService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.module.support.repeatsubmit.annoation.RepeatSubmit;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工作流流程模型管理
 *
 * @author KonBAI
 * @createTime 2022/6/21 9:09
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/model")
public class WfModelController {

    @Resource
    private  WfModelService modelService;
    @Resource
    private  WfCategoryService wfcategoryService;

    /**
     * 查询流程模型列表
     *
     * @param modelBo 流程模型对象
     */
    @SaCheckPermission("workflow:model:list")
    @GetMapping("/list")

    public ResponseDTO<PageResult<WfModelVo>> list(WfModelBo modelBo) {
        log.info("查询流程模型列表请求参数:{}", JSON.toJSONString(modelBo));
        PageResult<WfModelVo> page = modelService.listPage(modelBo);
        return ResponseDTO.ok(page);
    }

    /**
     * 查询流程模型列表
     *
     * @param modelBo 流程模型对象
     */
    @SaCheckPermission("workflow:model:list")
    @GetMapping("/historyList")
    public ResponseDTO<PageResult<WfModelVo>> historyList(WfModelBo modelBo) {
        PageResult<WfModelVo> page = modelService.historyList(modelBo);
        return ResponseDTO.ok(page);
    }

    /**
     * 获取流程模型详细信息
     *
     * @param modelId 模型主键
     */
    @SaCheckPermission("workflow:model:query")
    @GetMapping(value = "/{modelId}")
    public ResponseDTO<WfModelVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("modelId") String modelId) {
        return ResponseDTO.ok(modelService.getModel(modelId));
    }

    /**
     * 获取流程表单详细信息
     *
     * @param modelId 模型主键
     */
    @SaCheckPermission("workflow:model:query")
    @GetMapping(value = "/bpmnXml/{modelId}")
    public ResponseDTO<String> getBpmnXml(@NotNull(message = "主键不能为空") @PathVariable("modelId") String modelId) {
        String bpmnxml = modelService.queryBpmnXmlById(modelId);
        return ResponseDTO.ok(bpmnxml);
    }

    /**
     * 新增流程模型
     */
    @SaCheckPermission("workflow:model:add")
    @PostMapping
    public ResponseDTO<Void> add( @RequestBody WfModelBo modelBo) {
        modelService.insertModel(modelBo);
        return ResponseDTO.ok();
    }

    /**
     * 修改流程模型
     */
    @SaCheckPermission("workflow:model:edit")
    @PutMapping
    public ResponseDTO<Void> edit(@Validated @RequestBody WfModelBo modelBo) {
        modelService.updateModel(modelBo);
        return ResponseDTO.ok();
    }

    /**
     * 保存流程模型
     */
    @SaCheckPermission("workflow:model:save")

    @RepeatSubmit()
    @PostMapping("/save")
    public ResponseDTO<String> save(@RequestBody WfModelBo modelBo) {
        modelService.saveModel(modelBo);
        return ResponseDTO.ok();
    }

    /**
     * 设为最新流程模型
     *
     * @param modelId
     * @return
     */
    @SaCheckPermission("workflow:model:save")
    @RepeatSubmit()
    @PostMapping("/latest")
    public ResponseDTO<?> latest(@RequestParam String modelId) {
        modelService.latestModel(modelId);
        return ResponseDTO.ok();
    }

    /**
     * 删除流程模型
     *
     * @param modelIds 流程模型主键串
     */
    @SaCheckPermission("workflow:model:remove")
    @DeleteMapping("/{modelIds}")
    public ResponseDTO<String> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] modelIds) {
        modelService.deleteByIds(Arrays.asList(modelIds));
        return ResponseDTO.ok();
    }

    /**
     * 部署流程模型
     *
     * @param modelId 流程模型主键
     */
    @SaCheckPermission("workflow:model:deploy")
    @RepeatSubmit()
    @GetMapping("/deploy/{modelId}")
    public ResponseDTO<Boolean> deployModel(@NotNull(message = "主键不能为空") @PathVariable("modelId") String modelId) {
        log.info("部署流程模型:{}", modelId);
        Boolean res = modelService.deployModel(modelId);
        return ResponseDTO.ok(res);
    }

    /**
     * 导出流程模型数据
     */
    @SaCheckPermission("workflow:model:export")
    @PostMapping("/export")
    public void export(WfModelBo modelBo, HttpServletResponse response) {
        List<WfModelVo> list = modelService.list(modelBo);
        List<WfModelExportVo> listVo = BeanUtil.copyToList(list, WfModelExportVo.class);
        List<WfCategoryVo> categoryVos = wfcategoryService.queryList(new WfCategoryBo());
        Map<String, String> categoryMap = categoryVos.stream()
                .collect(Collectors.toMap(WfCategoryVo::getCode, WfCategoryVo::getCategoryName));
        for (WfModelExportVo exportVo : listVo) {
            exportVo.setCategoryName(categoryMap.get(exportVo.getCategory()));
        }
      //  ExcelUtil.exportExcel(listVo, "流程模型数据", WfModelExportVo.class, response);
    }
}
